import pymysql
from appmanger.lib.getlog import get_log
logger=get_log()

def api_db(sql):
    # api信息数据库地址
    try:
        api_db=pymysql.connect("127.0.0.1","root","123456","smartportal",charset='utf8')
        cursor = api_db.cursor()
        cur=cursor.execute(sql)
        if cursor.description == None or cursor.description =='':
            dbresult=cur
        else:
            columns = [col[0] for col in cursor.description]
            dbresult = [dict(zip(columns, row)) for row in cursor.fetchall()]
        api_db.commit()
        cursor.close()
        api_db.close()
        logger.info("SQL处理成功！")
        return dbresult
    except Exception as e:
        logger.error("SQL处理异常，异常信息为%s"%e)
        return e